namespace Pronghorn.Core
{
	public class IfNodeVisitor : INodeVisitor
	{
		#region Static Fields

		private static string _ifStart = "<!--#{if";

		#endregion Static Fields

		#region Private Methods

		private static bool isIf(string line)
		{
			return line.Contains(_ifStart);
		}

		private bool isIfEnd(string line)
		{
			return line.Contains("<!--#{endif}-->");
		}

		private string parseEnd(string line)
		{
			if (!isIfEnd(line)) return line; 
			return "}\r\n";
		}

		private string parseStart(string line)
		{
			if (!isIf(line)) return line;
			var item = line.Replace(_ifStart, "").Replace("}-->", "").Trim();
			return string.Format("if (Model.Data.IsTrue(\"{0}\"))",item) + "{\r\n";
		}

		#endregion Private Methods

		#region Public Methods

		public void Visit(ISkinNodeParser skinNodeParser)
		{
			skinNodeParser.Line = parseStart(skinNodeParser.Line);
			skinNodeParser.Line = parseEnd(skinNodeParser.Line);
		}

		#endregion Public Methods
	}
}